package com.android.mios;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.android.mios.adapter.CameraGridViewAdapter;
import com.android.mios.intenet.PollLoop;
import com.android.mios.model.Category;
import com.android.mios.model.Device;
import com.android.mios.util.Global;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.GridView;

public class CameraControlActivity extends DeviceBaseActivity {

	private List<Device> camearOfArray;
	private GridView mGridView;
	private CameraGridViewAdapter mCameraGridViewAdapter;
	public static final String DEVICE_ID = "com.android.miso.DEVICE_ID";
	public static final String DEVICE_NAME = "com.android.miso.DEVICE_NAME";
	public static final String SERIAL_NUMBER = "com.android.miso.SERIAL_NUMBER";

	private static final String CAMERA_FULL_IMAGE_ACTIVITY = "CameraFullImageActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.camera_control_layout);
		initData();
		initView();
	}

	private void initData() {
		camearOfArray = new ArrayList<Device>();
		mCameraGridViewAdapter = new CameraGridViewAdapter(this, camearOfArray,
				R.layout.camera_item);
	}

	private void initView() {
		initGridView();
	}

	private void initGridView() {

		mGridView = (GridView) findViewById(R.id.GV_CameraControl);
		mGridView.setAdapter(mCameraGridViewAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub

				Device camera = camearOfArray.get(position);
				LinearLayout container = (LinearLayout) ((ActivityGroup) CameraControlActivity.this
						.getParent()).getWindow().findViewById(R.id.container);

				container.removeAllViews();

				Intent intent = new Intent(CameraControlActivity.this,
						CameraFullImageActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.putExtra(CameraControlActivity.DEVICE_ID, camera.id);
				intent.putExtra(CameraControlActivity.DEVICE_NAME,
						camera.toString());
				intent.putExtra(CameraControlActivity.SERIAL_NUMBER,
						camera.serialNumber);

				LocalActivityManager localActivityManager = ((ActivityGroup) CameraControlActivity.this
						.getParent()).getLocalActivityManager();
//				localActivityManager.destroyActivity(
//						CAMERA_FULL_IMAGE_ACTIVITY, true);

				container.addView(
						localActivityManager.startActivity(
								CAMERA_FULL_IMAGE_ACTIVITY, intent)
								.getDecorView(), new LinearLayout.LayoutParams(
								LayoutParams.FILL_PARENT,
								LayoutParams.FILL_PARENT));

			}
		});
	}

	@Override
	protected List<Device> getDeviceList(int serialNumber, int roomId) {

		Map<String, Device> allDevices = PollLoop.getInstance().getDevices();
		List<Device> devices = Device.findUnHideDevicesByCategory(allDevices,
				Category.CAMERA);
		if (roomId != -1)// -1 is all room
			devices = Device.findDevicesByRoom(devices, serialNumber, roomId);
		return devices;
	}

	@Override
	protected void updateDevice() {
		// TODO Auto-generated method stub
		camearOfArray.clear();
		camearOfArray.addAll(getDeviceList(Global.getInstance()
				.getSerialNumber(), Global.getInstance().getRoomID()));
		mCameraGridViewAdapter.notifyDataSetChanged();
	}
}
